<?php
/**
 * Project: Recipe
 * File: SetMailAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-5-3
 * Time: 上午8:49
 */

	class SetMailAction extends CommonAction{

		function index(){
			$this->display('mailconfig');
		}

		function mailconfig(){
			$this->display();
		}


		function setemailconfig(){
			$siteinfo = $_POST;
			$file = $siteinfo['filename'];
			if($file=="mailconfig.inc.php"){
				unset($siteinfo['filename']);
				unset($siteinfo['__hash__']);
				if($siteinfo['emailserver']==''){$siteinfo['emailserver']='smtp.163.com';};
				if($siteinfo['emailport']==''){$siteinfo['emailport']='25';};
				if($siteinfo['fromemail']==''){$siteinfo['fromemail']='franch_test@163.com';};
				if($siteinfo['fromemailalias']==''){$siteinfo['fromemailalias']='franch_test';};
				if($siteinfo['password']==''){$siteinfo['password']='123456abc';};
				$content = "<?php \n\n\treturn array(\n";
				foreach($siteinfo as $key=>$value){
					$key = strtoupper($key);
					if(!strtolower($value) || is_numeric($value)){
						$content.="\t'$key'=>$value,\n";
					}else{
						$content.="\t'$key'=>'$value',\n";
					}
				}
				$content.="\n);";
				$f = chmod($file, 0755);
				$put = file_put_contents($file, $content);
				if($put){
					$this->success('Modify email configuration success');

				}else{
					$this->error('Modify the configuration files of the system fail');
				}
			}else{
				$this->error('The configuration file does not exist');
			}
		}

		function sendmailview(){
			if(isset($_GET['rid']) && !empty($_GET['rid'])){
				$role = M('Role');
				$roleinfo = $role->field('name')->find($_GET['rid']);
				$roleuser = M('RoleUser');
				$roleuserinfo = $roleuser->where('roleid='.$_GET['rid'])->select();
				$send = '【 '.$roleinfo['name'].' 】分组里的所有会员';
				if(!empty($roleuserinfo)){
					$this->assign('send', $send);
				}
			}

			if(isset($_GET['uid']) && !empty($_GET['uid'])){
				$user = M('User');
				$userinfo = $user->field('username,email')->find($_GET['uid']);
				$this->assign('userinfo', $userinfo);
			}

			$this->display();
		}

		function sendmail(){
			if(C('ENABLE')==1){
				import("ORG.Net.phpmailer");
				$mail = new PHPMailer(true);
				$mail->IsSMTP();
				$mail->SMTPAuth   = true;
				$mail->Port       = C('EMAILPORT');
				$mail->Host       =C('EMAILSERVER');
				$mail->Username   = C('FROMEMAIL');
				$mail->Password   = C('PASSWORD');
				$mail->AddReplyTo(C('FROMEMAIL'),C('FROMEMAILALIAS'));
				$mail->From       = C('FROMEMAIL');
				$mail->FromName   = C('FROMEMAILALIAS');
				$mail->IsHTML(true);

				if(isset($_POST['rid']) && !empty($_POST['rid'])){
					$roleuser = D('RoleUser');
					$roleuserinfo = $roleuser->relation(true)->where('roleid='.$_POST['rid'])->select();
					foreach($roleuserinfo as $key=>$value){
						$smtpemailto=$value['userinfo']['email'];
						$username=$value['userinfo']['username'];
						$mail->AddAddress($smtpemailto);
						$mail->Subject  = $_POST['title'];
						$body = $_POST['content'];
						$body = str_replace('%username%', $username, $body);
						$body = str_replace('%date%', date('Y-m-d H:i:s'), $body);
						$mail->MsgHTML($body);
						$mail->Send();
					}
					$this->success('发送成功');
				}

				if(isset($_POST['uid']) && !empty($_POST['uid'])){
					$user = M('User');
					$userinfo = $user->field('username,email')->find($_POST['uid']);
					$smtpemailto=$userinfo['email'];
					$username=$userinfo['username'];
					$mail->AddAddress($smtpemailto);
					$mail->Subject  = $_POST['title'];
					$body = $_POST['content'];
					$body = str_replace('%username%', $username, $body);
					$body = str_replace('%date%', date('Y-m-d H:i:s'), $body);
					$mail->MsgHTML($body);
					if($mail->Send()){
						$this->success('发送成功',__APP__.'/User/index');
					}else{
						$this->error('发送失败');
					}
				}
			}else{
				$this->error('邮件功能未开启');
			}
		}

		function mailactive(){
			$setmail = M('SetEmail');
			$setmailinfo = $setmail->find(1);
			$this->assign('data', $setmailinfo);
			$this->display();
		}

		function mailsuccess(){
			$setmail = M('SetEmail');
			$setmailinfo = $setmail->find(2);
			$this->assign('data', $setmailinfo);
			$this->display();
		}

		function sendrecipe(){
			$setmail = M('SetEmail');
			$setmailinfo = $setmail->find(3);
			$this->assign('data', $setmailinfo);
			$this->display();
		}

		function sendpassword(){
			$setmail = M('SetEmail');
			$setmailinfo = $setmail->find(4);
			$this->assign('data', $setmailinfo);
			$this->display();
		}

		function update(){
			if(isset($_POST['id']) && !empty($_POST['id']) ){
				$setmail = D('SetEmail');
				if($data = $setmail->create()){
					$setmail->save();
					$this->success('修改成功');
				}else{
					$this->error($setmail->getError());
				}
			}else{
				$this->error('异常操作');
			}
		}

	}